package org.example.yolodemo.controller;

import org.example.yolodemo.entity.ImageDetection;
import org.example.yolodemo.service.ImageDetectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/detections")
public class ImageDetectionController {
    private final ImageDetectionService service;

    @Autowired
    public ImageDetectionController(ImageDetectionService service) {
        this.service = service;
    }

    @GetMapping
    public String getAllDetections(Model model) {
        List<ImageDetection> detections = service.getAllDetections();
        System.out.println(detections);
        model.addAttribute("detections", detections);
        return "detections";
    }

    @PostMapping("/update/{id}")
    public String updateDetection(@PathVariable Long id,
                                  @ModelAttribute ImageDetection updatedDetection) {
        service.updateDetection(id,
                updatedDetection.getImageName(),
                updatedDetection.getX1(),
                updatedDetection.getY1(),
                updatedDetection.getX2(),
                updatedDetection.getY2());
        return "redirect:/detections";
    }

    @PostMapping("/delete/{id}")
    public String deleteDetection(@PathVariable Long id) {
        service.deleteDetection(id);
        return "redirect:/detections";
    }
}